﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.UI;
using OutlookHub.Styles;
using Silvermoon.Classes;
using Microsoft.WindowsMobile.PocketOutlook;
using Silvermoon.Windows;
using Silvermoon.OpenGL;
using Silvermoon.Images;
using OutlookHub.Utils;

namespace OutlookHub.Pages
{
    public class TaskDetailsPage : Page
    {
        private Label subject, duration, info, details, categories;
        private Task task;

        public TaskDetailsPage()
            : base()
        {
            InitializePage();
        }

        private void InitializePage()
        {
            SetTransition(350, TransitionMask.RotateLeftIn, TransitionMask.RotateLeftIn);
            Styles = StylePool.TaskDetails;

            Title = "TASK";
            ScrollStackPanel panel = new ScrollStackPanel { Margin = new Margin(0) };
            subject = new Label { FontScale = 1.3f, Font = SystemFonts.Big, AutoSize = true, Margin = new Margin(4, 4, 4, 0), LineMode = LineMode.WordWrap };
            duration = new Label { TextColor = PhoneColors.Accent, AutoSize = true, Margin = new Margin(4, 0, 4, 4), LineMode = LineMode.WordWrap };
            details = new Label { AutoSize = true, Margin = new Margin(4, 24, 4, 4), LineMode = LineMode.WordWrap, Font = SystemFonts.Normal, TextColor = PhoneColors.Light };
            categories = new Label { AutoSize = true, Margin = new Margin(4, 14, 4, 4), LineMode = LineMode.WordWrap, Font = SystemFonts.Normal, FontScale = 0.75f, Skew = 2 };
            info = new Label { AutoSize = true, Margin = new Margin(4, 32, 4, 4), LineMode = LineMode.WordWrap, Font = SystemFonts.Medium };
            panel.Controls.Add(subject, duration, details, categories, info);
            Controls.Add(panel);

            Toolbar tb = new Toolbar();
            tb.AddButton(SysImages.Back, Page.BackCommand);
            tb.AddButton(SysImages.Edit).Tap += new EventHandler<TapEventArgs>(TaskDetailsPage_Tap);

            Toolbar = tb;
        }

        void TaskDetailsPage_Tap(object sender, TapEventArgs e)
        {
            Task copy = new Task();
            copy.Copy(task);
            EditTaskDetailsPage page = new EditTaskDetailsPage() { Task = copy };
            page.Accept += (s, ee) =>
                {
                    task.Copy(ee.Task);
                    task.Update();
                };
            this.NavigateTo(page);
        }

        public Task Task
        {
            get { return task; }
            set
            {
                if (task != value)
                {
                    task = value;
                    OnTaskChanged();
                }
            }
        }

        private void OnTaskChanged()
        {
            subject.Text = task.Subject;
            duration.Text = task.ToDuration();
            info.Text = task.Body;
            details.Text = CreateDetails(task);
            categories.Text = task.Categories;
            categories.Visible = !string.IsNullOrEmpty(task.Categories);
        }

        private string CreateDetails(Task task)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Importance: " + task.Importance.ToString());
            sb.AppendLine("Sensitivity: " + task.Sensitivity.ToString());
            sb.Append(task.Complete ? "Completed" : "Incomplete");

            return sb.ToString();
        }

        public override object OnSaveViewState()
        {
            return task;
        }

        public override void OnLoadViewState(object viewState)
        {
            Task = viewState as Task;
        }
    }
}
